﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using ZLCMS.Data.Model;
using System.Web.UI.WebControls;
using NHibernate;

using NHibernate.Criterion;
using DotNet.Common.NHibernateUtil;
using ZLCMS.Business.Util;
using Newtonsoft.Json;
using System.Text;
using ZLCMS.Business.WeixinService.Data;

namespace ZLCMS.Web.Interface.weixin.Html
{
    public partial class WxDataApplyDoc : UrlRewritePage
    {
        protected WxApplyConfig _applyDb;
        protected List<WxApplyConfigItem> _itemList;

        protected string StatusMessage;

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                using (ISession session = SessionFactoryHelper.CreateSession())
                {
                    if (Request["account"] == null || Request["uid"] == null)
                    {
                        Response.Redirect("/Interface/weixin/html/WxErrorOccur.aspx");
                    }

                    _applyDb = session.CreateCriteria<WxApplyConfig>().Add(Expression.Eq("OwnerAccount", Request["account"])).Add(Expression.Eq("Id", int.Parse(Request["uid"]))).UniqueResult<WxApplyConfig>();

                    if (_applyDb == null)
                    {
                        Response.Redirect("/Interface/weixin/html/WxErrorOccur.aspx");
                    }

                }
            }
            catch (System.Exception ex)
            {
                Response.Redirect("/Interface/weixin/html/WxErrorOccur.aspx");
            }
        }

        protected string CreateOption()
        {
            if (_applyDb == null)
            {
                return null;
            }

            StringBuilder strBuilder = new StringBuilder();

            strBuilder.Append(CreateOptionItem(JsonConvert.DeserializeObject<WxApplyConfigItem>(_applyDb.ApplyItem1 == null ? "" : _applyDb.ApplyItem1), 1));
            strBuilder.Append(CreateOptionItem(JsonConvert.DeserializeObject<WxApplyConfigItem>(_applyDb.ApplyItem2 == null ? "" : _applyDb.ApplyItem2), 2));
            strBuilder.Append(CreateOptionItem(JsonConvert.DeserializeObject<WxApplyConfigItem>(_applyDb.ApplyItem3 == null ? "" : _applyDb.ApplyItem3), 3));
            strBuilder.Append(CreateOptionItem(JsonConvert.DeserializeObject<WxApplyConfigItem>(_applyDb.ApplyItem4 == null ? "" : _applyDb.ApplyItem4), 4));
            strBuilder.Append(CreateOptionItem(JsonConvert.DeserializeObject<WxApplyConfigItem>(_applyDb.ApplyItem5 == null ? "" : _applyDb.ApplyItem5), 5));
            strBuilder.Append(CreateOptionItem(JsonConvert.DeserializeObject<WxApplyConfigItem>(_applyDb.ApplyItem6 == null ? "" : _applyDb.ApplyItem6), 6));
            strBuilder.Append(CreateOptionItem(JsonConvert.DeserializeObject<WxApplyConfigItem>(_applyDb.ApplyItem7 == null ? "" : _applyDb.ApplyItem7), 7));
            strBuilder.Append(CreateOptionItem(JsonConvert.DeserializeObject<WxApplyConfigItem>(_applyDb.ApplyItem8 == null ? "" : _applyDb.ApplyItem8), 8));
            strBuilder.Append(CreateOptionItem(JsonConvert.DeserializeObject<WxApplyConfigItem>(_applyDb.ApplyItem9 == null ? "" : _applyDb.ApplyItem9), 9));
            strBuilder.Append(CreateOptionItem(JsonConvert.DeserializeObject<WxApplyConfigItem>(_applyDb.ApplyItem10 == null ? "" : _applyDb.ApplyItem10), 10));

            return strBuilder.ToString();
        }

        private string CreateOptionItem(WxApplyConfigItem item, int i)
        {
            if (item == null)
            {
                return null;
            }
            if (item.IsAvailable != "1")
            {
                return null;
            }
            switch (item.Type)
            {
                case "str":
                case "phone":
                case "email": return CreateTextItem(item, i);
                case "rbox": return CreateRBoxItem(item, i);
                case "cbox": return CreateCBoxItem(item, i);
                case "ddlist": return CreateDDListItem(item, i);
            }
            return null;
        }


        private string CreateRBoxItem(WxApplyConfigItem item, int i)
        {
            StringBuilder strBuilder = new StringBuilder();

            strBuilder.Append("<fieldset data-role=\"controlgroup\" data-mini=\"true\" command-param='ApplyItem" + i + "'>");
            strBuilder.Append("<legend>" + item.Title + ":</legend>");

            string[] dataList = item.OptionalData.Split(',');

            int index = 0;

            foreach (string dataItem in dataList)
            {
                strBuilder.Append("<input name=\"rbox" + i + "\" id=\"rbox-v-" + index + "\" value=\"" + dataItem + "\"type=\"radio\" ");
                if (index == 0)
                {
                    strBuilder.Append("data-rule=\"" + CreateRuleString(item) + "\"");
                }
                strBuilder.Append(" >");
                strBuilder.Append("<label for=\"rbox-v-" + index + "\">" + dataItem + "</label>");
                index++;
            }

            strBuilder.Append("</fieldset>");
            strBuilder.Append("<div><span class=\"msg-box\" for=\"rbox" + i + "\"></span></div>");

            return strBuilder.ToString();
        }

        private string CreateDDListItem(WxApplyConfigItem item, int i)
        {
            StringBuilder strBuilder = new StringBuilder();

            strBuilder.Append("<fieldset data-role=\"controlgroup\" data-mini=\"true\" command-param='ApplyItem" + i + "'>");
            strBuilder.Append("<legend>" + item.Title + ":</legend>");
            strBuilder.Append("<select name=\"select-native-5\" id=\"select-native-" + i + "\" data-rule=\"" + CreateRuleString(item) + "\" command-param=\"ApplyItem" + i + "\" >");
            string[] dataList = item.OptionalData.Split(',');

            int index = 0;
            strBuilder.Append("<option value=\"-1\">--请选择--</option>");
            foreach (string dataItem in dataList)
            {
                var str = Server.HtmlEncode(dataItem);
                strBuilder.Append("<option name=\"ddlist" + i + "\" id=\"ddlist-v-" + index + "\" value=\"" + str + "\" >" + str + "</option>");
                index++;
            }
            strBuilder.Append("</select>");
            strBuilder.Append("</fieldset>");
            strBuilder.Append("<div><span class=\"msg-box\" for=\"select-native-" + i + "\"></span></div>");

            return strBuilder.ToString();

        }

        private string CreateCBoxItem(WxApplyConfigItem item, int i)
        {
            StringBuilder strBuilder = new StringBuilder();

            strBuilder.Append("<fieldset data-role=\"controlgroup\" data-mini=\"true\" command-param='ApplyItem" + i + "'>");
            strBuilder.Append("<legend>" + item.Title + ":</legend>");

            string[] dataList = item.OptionalData.Split(',');

            int index = 0;

            foreach (string dataItem in dataList)
            {
                strBuilder.Append("<input name=\"cbox" + i + "\" id=\"cbox-v-" + index + "\" type=\"checkbox\" data-rule=\"" + CreateRuleString(item) + "\" value=\"" + dataItem + "\" >");
                strBuilder.Append("<label for=\"cbox-v-" + index + "\">" + dataItem + "</label>");
                index++;
            }

            strBuilder.Append("</fieldset>");
            strBuilder.Append("<div><span class=\"msg-box\" for=\"cbox" + i + "\"></span></div>");

            return strBuilder.ToString();
        }

        /// <summary>
        /// 创建文本框HTML代码
        /// </summary>
        /// <param name="item"></param>
        /// <param name="i"></param>
        /// <returns></returns>
        private string CreateTextItem(WxApplyConfigItem item, int i)
        {
            StringBuilder strBuilder = new StringBuilder();
            strBuilder.Append("<label for=\"text-basic\">" + item.Title + ":</label>");
            strBuilder.Append("<input name=\"ApplyItem" + i + "\" value=\"\" command-param='ApplyItem" + i + "' type=\"" + CreateTypeString(item) + "\" data-rule=\"" + CreateRuleString(item) + "\" />");
            strBuilder.Append("<div><span for=\"ApplyItem" + i + "\" class=\"msg-box\"></span></div>");

            return strBuilder.ToString();
        }

        /// <summary>
        /// 生成录入框类型参数
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        private string CreateTypeString(WxApplyConfigItem item)
        {
            string typeString;

            switch (item.Type)
            {
                case "str": typeString = "text"; break;
                case "phone":
                case "email": typeString = "tel"; break;
                default: typeString = "text"; break;
            }
            return typeString;
        }

        /// <summary>
        /// 生成规则参数
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        private string CreateRuleString(WxApplyConfigItem item)
        {
            bool isFirst = true;

            string ruleString = null;

            if (item.IsRequired == "1")
            {
                switch (item.Type)
                {
                    case "str":
                    case "phone":
                    case "email": ruleString += "required"; isFirst = false; break;
                    case "rbox": ruleString += "checked"; break;
                    case "cbox": ruleString += "checked"; break;
                    case "ddlist": ruleString += "selectval"; break;
                }
            }

            switch (item.Type)
            {
                case "phone": ruleString += isFirst ? "" : ":" + "mobile"; break;
                case "email": ruleString += isFirst ? "" : ":" + "email"; break;
            }
            return ruleString;
        }

    }
}